package com.shanhai.wallpapers.di

import com.shanhai.wallpapers.network.api.PexelsApi
import okhttp3.OkHttpClient
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

val networkModule = module {
    factory { provideOkHttpClient() }
    factory { providePexelsApi(retrofit = get()) }
    single { provideRetrofit(okHttpClient = get()) }
}

fun provideOkHttpClient(): OkHttpClient {
    return OkHttpClient.Builder()
//        .proxy(
//            Proxy(
//                Proxy.Type.HTTP,
//                InetSocketAddress("127.0.0.1", 8080)
//            )
//        )
        .connectTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS)
        .retryOnConnectionFailure(false).build()
}

fun providePexelsApi(retrofit: Retrofit): PexelsApi {
    return retrofit.create(PexelsApi::class.java)
}

fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder().client(okHttpClient).baseUrl("https://api.pexels.com/v1/")
        .addConverterFactory(GsonConverterFactory.create()).build()
}